package com.llkj.qingnianguoxue.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.WorkListAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.MusicLikeBean;
import com.llkj.qingnianguoxue.bean.WorkListBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.music.MusicHomeActivity;
import com.llkj.qingnianguoxue.music.RecordCompleteActivity;
import com.llkj.qingnianguoxue.music.RecordMusicActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WorkListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView lvWorks;
    private WorkListAdapter mWorkAdapter;
    private List<WorkListBean> mWorkData;
    private int totalSize, mPage,mCurrentPosition;
    private String mUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_list);
        setTitle("演唱作品", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        mUserId = intent.getStringExtra("uid");

        initView();

        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvWorks.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mWorkData = new ArrayList<>();
        mWorkAdapter = new WorkListAdapter(R.layout.item_work_list,mWorkData);
        lvWorks.setAdapter(mWorkAdapter);

        refreshLayout.setOnRefreshListener(this);
        mWorkAdapter.setOnLoadMoreListener(this);

        lvWorks.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                WorkListBean item = mWorkData.get(i);
                String uid = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_ID);
//                if(!uid.equals(mUserId)){
//                    RecordMusicActivity.startActivity(WorkListActivity.this,item.getId(),item.getSing_img()
//                            ,item.getName(),item.getPlay_num(),item.getSing(),item.getMp3(),item.getLyric(),item.getSing_name());
//                }else{
                    Bundle bundle = new Bundle();
                    bundle.putString("categoryType","1");
                    bundle.putString("musicLrcUrl",item.getLyric());//歌词链接
                    bundle.putString("recordPath",item.getSing());//录制文件地址
                    bundle.putString("musicCoverUrl",item.getSing_img());//伴奏音乐封面图片
                    bundle.putString("musicName",item.getName());//伴奏音乐名称
                    bundle.putString("countSing",item.getPlay_num());//演唱人数
                    bundle.putString("musicBgPath",item.getBg_img());//背景图片地址
                    startActivity(RecordCompleteActivity.class,bundle);
//                }
            }
        });
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getData();
    }

    private void getData(){
        HttpMethod.kojiSing(this,this,mUserId,null,String.valueOf(mPage));
    }
    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (HttpMethod.HTTP_KOJI_SING == method) {//
            ResponseList<WorkListBean> response = GsonUtil.fromJsonList(data,WorkListBean.class);
            if(response!=null){
                totalSize = Integer.parseInt(response.getNum());
                if (mPage == 1) {
                    mWorkData.clear();
                    LoadDataHelper.refreshComplete(mWorkAdapter, mWorkData, refreshLayout, lvWorks);
                } else {
                    mWorkAdapter.setEnableLoadMore(true);
                    mWorkAdapter.loadMoreComplete();
                }
                mWorkData.addAll(response.getLike());
                mWorkAdapter.notifyDataSetChanged();
            }
        }
    }
    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mWorkAdapter, null, lvWorks, totalSize);
    }
}
